<?php
/**
 * Lớp hỗ trợ các vấn đề về chuỗi
 * @author Mr.UBKey
 */
class XPHP_String
{
	
	/**
	 * Mã hóa MD5 cho chuỗi theo quy tắc riêng
	 * @param string $str Chuỗi cần mã hóa
	 */
	public static function md5Encode($str)
	{
		return md5(md5($str."xphp.v1.2010"));
	}
	
	/**
	 * Phương thức tạo chuỗi ngẫu nhiên
	 * @param int $num Số kí tự của chuỗi
	 */
	public static function randomString($num) 
	{	
	    $characters = 'QWERTYUIOPLKJHGFDSAZXCVBNM0123456789qwertyuioplkjhgfdsazxcvbnm';
	    
	    $string = '';    
	    for ($p = 0; $p < $num; $p++) {
	        $string .= $characters[mt_rand(0, strlen($characters)-1)];
	    }
	    return $string;
	}
	
	public static function secure($s_str)
	{
		//HtmlSpecialCharsEncode
		$s_str = self::HtmlStringEncode(stripslashes($s_str));
		//Script, blocked
        $s_str = str_ireplace("script", "blocked", $s_str);
        //Sql inject
        $s_str = mysql_escape_string($s_str);
        
        return $s_str;
	}
	
	public static function HtmlStringEncode($html)
	{
		$out = htmlspecialchars(
          html_entity_decode($html, ENT_QUOTES, 'UTF-8'), 
          ENT_QUOTES, 'UTF-8'
        );
		return $out;
	}
	
	public static function HtmlStringDecode($html)
	{
		$out = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
		
		return $out;
	}
	
	public static function HtmlEncode($html)
	{
		return htmlentities($html);
	}
	
	public static function HtmlDecode($html)
	{
		return html_entity_decode($html);
	}
	
	public static function trim_all($string)
	{
		$arrChars = array("\t", "\n", "\r");
		foreach ($arrChars as $c)
		{
			$string = str_replace($c, "", $string);
		}
		return $string;
	}
}